import json


def load_flow_json() -> dict:
    import os
    import binascii
    candidates = [
        os.path.join(os.getcwd(), 'examples', 'show_message_flow.json'),
        'e:\\work\\work_ai\\flow-engine\\examples\\show_message_flow.json',
        os.path.join(os.getcwd(), 'demoFlow.JSON'),
        'E:\\work\\work_ai\\flowEngineDemo\\demoFlow.JSON'
    ]
    last_err = None
    for p in candidates:
        try:
            with open(p, 'rb') as fb:
                raw = fb.read()
            print(f'[DEBUG] Read bytes from {p}: size={len(raw)} first16={binascii.hexlify(raw[:16]).decode()}')
            text = raw.decode('utf-8-sig', errors='replace').lstrip('\ufeff')
            print(f'[DEBUG] Decoded text length={len(text)} startswith={text[:1]!r}')
            flow = json.loads(text)
            print(f'[INFO] Loaded flow from {p}')
            return flow
        except Exception as e:
            print(f'[WARN] Failed to parse {p}: {e}')
            last_err = e
    raise RuntimeError(f'Failed to load flow JSON. Last error: {last_err}')